package com.ce.pms.house.web;


import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.house.service.IGoOutService;
import com.ce.pms.house.vo.ReasonVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;
/**
 *
 * 逻辑、功能相关描述:退伙设置
 *
 * @author 燕大为 编写日期：2008-6-26
 */
public class PartnershipAction extends PmsBaseAction {

	private IGoOutService goOutService;

	/***
	 * Function: 初始化退伙设置主界面
	 * @return
	 * Create author:燕大为
	 * Create on:2008-6-26
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initPartnershipMain()
	{
		return "initPartnershipMain";
	}


	/***
	 * 功能描述:  验证该异常名称是否以存在<br>
	 * @param name
	 * @param oldName
	 * @param type
	 * Create author:yandawei <br>
	 * Create on:2008-7-24  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkReasonByName(
			@Read(key = "name") String name,
			@Read(key = "oldId") Long id,
			@Read(key = "oldType") String type)
	{
		ReasonVO reason = new ReasonVO();
		reason.setId(id);
		reason.setName(name);
		reason.setTypes(type);
		String result = "true";
		boolean unique = goOutService.checkUnique(reason);
		if(!unique){
			result = "已经存在";
			if(!BlankUtil.isBlank(id)){
			    if(goOutService.checkUsed(reason))
				   result = "该记录以被引用,不能修改";
			}
		}
		renderText(result);
	}

	/***
	 * 功能描述: 验证记录是否在退伙中引用(前台删除时不做验证,交给后台处理)<br>
	 * @param id 记录ID
	 * Create author:yandawei <br>
	 * Create on:2008-7-24  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkReferenceReason(@Read(key="id") Long id)
	{
		if(!BlankUtil.isBlank(id))
		{
			ReasonVO reason = new ReasonVO();
			reason.setId(id);
			boolean referenced = goOutService.checkUsed(reason);
			if(!referenced){
				renderText("true");
			}else{
				renderText("该记录以被引用,不能删除");
			}
		}
	}


	/***
	 * Function: 初始化退伙异常新增界面(抄表异常,验房异常,物品移交异常)
	 * @param type
	 * @return
	 * Create author:燕大为
	 * Create on:2008-6-27
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initReasonAdd(@Read(key = "type") String type)
	{
		String url = "";
		if(SysrefcodeDO.CS_REASON_DEF_COPY_METER_REASON.equals(type))
		{
			url = "copyMeterEdit";
		}
		else if(SysrefcodeDO.CS_REASON_DEF_CHECK_HOUSE_REASON.equals(type))
		{
			url = "checkHouseEdit";
		}
		else if(SysrefcodeDO.CS_REASON_DEF_GOODS_TRANSFER_REASON.equals(type))
		{
			url = "goodsTransferEdit";
		}
		setAttribute("editFlag", "add");
		return url;
	}



	/***
	 * Function: 初始化退伙异常修改界面(抄表异常,验房异常,物品移交异常)
	 * @param reasonId
	 * @param type
	 * @return
	 * Create author:燕大为
	 * Create on:2008-6-27
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initReasonEdit(@Read(key = "id") Long id
								,@Read(key = "type") String type)
	{
		String url = "";
		if(SysrefcodeDO.CS_REASON_DEF_COPY_METER_REASON.equals(type))
		{
			url = "copyMeterEdit";
		}
		else if(SysrefcodeDO.CS_REASON_DEF_CHECK_HOUSE_REASON.equals(type))
		{
			url = "checkHouseEdit";
		}
		else if(SysrefcodeDO.CS_REASON_DEF_GOODS_TRANSFER_REASON.equals(type))
		{
			url = "goodsTransferEdit";
		}
		setAttribute("editFlag", "update");
		ReasonVO reason = goOutService.findGoOutException(id);
		setAttribute("reason", reason);
		return url;

	}



	/***
	 * Function: 初始化异常Tab块列表页面
	 * @return
	 * Create author:燕大为
	 * Create on:2008-6-26
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initReasonList()
	{
		setAttribute("totalRows",new Integer(0));
		return "reasonList";

	}



	/***
	 * Function: 新增退伙异常信息
	 * @param reason
	 * Create author:燕大为
	 * Create on:2008-6-26
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void addReason(@Read ReasonVO reason)
	{
		reason.setOperator(getUserId());
		String result = goOutService.addGoOutException(reason);
		if("true".equals(result))
		{
			this.renderText("true");
		}
		else
		{
			this.renderText("false");
		}
	}



	/***
	 * Function: 修改退伙异常信息
	 * @param reason
	 * Create author:燕大为
	 * Create on:2008-6-27
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void editReason(@Read ReasonVO reason)
	{
		reason.setOperator(getUserId());
		String result = goOutService.editGoOutException(reason);
		if("true".equals(result))
		{
			this.renderText("true");
		}
		else
		{
			this.renderText("false");
		}
	}


	/***
	 * Function:删除退伙异常记录
	 * @param id
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void deleteReason(@Read(key="id") Long id)
	{
		String result = goOutService.deleteGoOutException(id);
		if("true".equals(result))
		{
			this.renderText("true");
		}
		else
		{
			this.renderText("false");
		}
	}


	/***
	 * Function: 根据类型获取退伙异常详细信息
	 * @param type
	 * @param id
	 * @return
	 * Create author:燕大为
	 * Create on:2008-6-27
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String getReasonDetail(
			@Read(key="type") String type,
			@Read(key="id") Long id)
	{
		ReasonVO reason = goOutService.findGoOutException(id);
		setAttribute("reason",reason);
		setAttribute("type", type);
		return "reasonDetail";
	}


	/***
	 * Function: 异常信息查询
	 * @param reason
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String findAllReasonBySearch(@Read ReasonVO reason,
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd",defaultValue = "15")Integer pageSize)
	{
		String sortStr=ExtremeUtils.getOrderString(this.getRequest());
		reason.setOrderString(sortStr);
		if(!BlankUtil.isBlank(reason.getTypes()))
		{
			String type = matchType(Long.parseLong(reason.getTypes()));
			reason.setTypes(type);
		}
		Page page = goOutService.showGoOutExceptions(reason, pageNO, pageSize);
		setAttribute("reasonList", page.getResult());
		setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));
		return "reasonList";
	}


	/***
	 * Function:初始化物品移交登记新增界面
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initGoodsRecordAdd()
	{
		setAttribute("editFlag", "add");
		return "goodsRecordEdit";
	}


	/***
	 * Function: 初始化物品移交登记修改界面
	 * @param id
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initGoodsRecordEdit(
			 @Read(key = "id") Long id)
	{
		setAttribute("editFlag", "update");
		ReasonVO goodsRecord = goOutService.findGoodsRecord(id);
		setAttribute("goodsRecord", goodsRecord);
		return "goodsRecordEdit";
	}


	/***
	 * Function: 初始化物品移交Tab块列表页面
	 * @return
	 * Create author:燕大为
	 * Create on:2008-6-26
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initGoodsRecordList()
	{
		setAttribute("totalRows",new Integer(0));
		return "goodsList";
	}


	/***
	 * Function: 新增物品移交登记
	 * @param reason
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void addGoodsRecord(@Read ReasonVO reason)
	{
		reason.setOperator(getUserId());
		String result = goOutService.addGoodsRecord(reason);
		if("true".equals(result))
		{
			this.renderText("true");
		}
		else
		{
			this.renderText("false");
		}
	}


	/***
	 * Function: 修改物品移交登记
	 * @param reason
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void editGoodsRecord(@Read ReasonVO reason)
	{
		reason.setOperator(getUserId());
		String result = goOutService.editGoodsRecord(reason);
		if("true".equals(result))
		{
			this.renderText("true");
		}
		else
		{
			this.renderText("false");
		}
	}


	/***
	 * Function: 删除物品移交登记记录
	 * @param id
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void deleteGoodsRecord(@Read(key="id") Long id)
	{
		String result = goOutService.deleteGoodsRecord(id);
		if("true".equals(result))
		{
			this.renderText("true");
		}
		else
		{
			this.renderText("false");
		}
	}


	/***
	 * Function: 获取物品登记详细信息
	 * @param id
	 * @return
	 * Create author:燕大为
	 * Create on:2008-6-27
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String getGoodsRecordDetail(
			@Read(key="id") Long id)
	{
		ReasonVO goodsRecord = goOutService.findGoodsRecord(id);
		setAttribute("goodsRecord",goodsRecord);
		return "goodsRecordDetail";
	}


	/***
	 * Function: 移交物品信息查询
	 * @param reason
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String findAllGoodsRecordBySearch(@Read ReasonVO reason,
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd",defaultValue = "15")Integer pageSize)
	{
		String sortStr=ExtremeUtils.getOrderString(this.getRequest());
		reason.setOrderString(sortStr);
		Page page = goOutService.showGoodsRecords(reason, pageNO, pageSize);
		setAttribute("goodsRecordList", page.getResult());
		setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));
		return "goodsList";
	}



	public void setGoOutService(IGoOutService goOutService) {
		this.goOutService = goOutService;
	}


	/***
	 * Function: 匹配SYS_REF_CODE中的value值(对应code=CS_REASON_DEF_REASON_TYPE)
	 * @param typeId
	 * @return
	 * Create author:燕大为
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private String matchType(Long typeId)
	{
		SysrefcodeDO reasonType = sysrefcodeService.findSysrefcodeById(typeId);
		String result = "";
		if("001".equals(reasonType.getValue()))
		{
			result = SysrefcodeDO.CS_REASON_DEF_COPY_METER_REASON;
		}
		else if("002".equals(reasonType.getValue()))
		{
			result = SysrefcodeDO.CS_REASON_DEF_CHECK_HOUSE_REASON;
		}
		else if("003".equals(reasonType.getValue()))
		{
			result = SysrefcodeDO.CS_REASON_DEF_GOODS_TRANSFER_REASON;
		}
		return result;
	}




}
